package com.hilaver.dzmis.web.servlet.login;

import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.service.IValidatingImageService;
import com.hilaver.dzmis.service.impl.ValidatingImageServiceImpl;

/**
 * Servlet implementation class for Servlet: GetValidatingImageServlet
 *
 */
 public class GetValidatingImageServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    
	public GetValidatingImageServlet() {
		super();
	}   	
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		IValidatingImageService ivis = new ValidatingImageServiceImpl();
		response.setHeader("Pragma","No-cache");
		response.setHeader("Cache-Control","no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg;charset=utf-8");
		//Store the validating code in the session 
		request.getSession().setAttribute(Constants.SESSION_VALIDATING_CODE, ivis.getValidatingCode());
		//System.out.println(request.getSession().getAttribute(Constants.SESSION_VALIDATING_CODE));
		ImageIO.write(ivis.generateImage(), "JPEG", response.getOutputStream());
		response.getOutputStream().close();
		
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}   	  	    
}